﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xiaowei.Players;
using XiaoweiV2;
using XiaoweiVoiceAI;

namespace Xiaowei.Services
{
    public class PlayerService
    {
        private PlayerService()
        {
            musicPlayer.PlayerStateChanged += MusicPlayer_PlayerStateChanged;
            //videoPlayer.PlayerStateChanged += VideoPlayer_PlayerStateChanged;
            radioPlayer.PlayerStateChanged += RadioPlayer_PlayerStateChanged;
            Skill = new Skill() { Id = Skills.News, Name = "新闻", Type = 0 };
        }
        public Skill Skill { get; set; }
        private void MusicPlayer_PlayerStateChanged(object sender, PlayStateCombine e)
        {
            if (musicPlayer.PlayStateCombine.PlayerState == PlayerState.Play)
            {
                XiaoweiAI.Instance.ReportState(new Skill() { Id = Skills.Music, Name = "音乐", Type = 0 }, "unique_id=" + musicPlayer.PlayStateCombine.ID + "&type=cur", "", (XiaoweiV2.PlayMode)1, (PlayState)1, 20);
                ChosePlayerByType(PlayerType.Music);
                PlayerManager.Instance.Resumed2();
            }
        }

        private void VideoPlayer_PlayerStateChanged(object sender, PlayStateCombine e)
        {
            //if (videoPlayer.PlayStateCombine.PlayerState == PlayerState.Play)
            //{
            //    XiaoweiAI.Instance.ReportState(new Skill() { Id = Skills.Video, Name = "视频", Type = 0 }, videoPlayer.PlayStateCombine.ID, "", (XiaoweiV2.PlayMode)1, (PlayState)1, 0);
            //    ChosePlayerByType(PlayerType.Video);
            //    PlayerManager.Instance.Resumed2();
            //}
        }

        private void RadioPlayer_PlayerStateChanged(object sender, PlayStateCombine e)
        {
            if (radioPlayer.PlayStateCombine.PlayerState == PlayerState.Play)
            {
                XiaoweiAI.Instance.ReportState(Skill, e.ID, e.Title, (XiaoweiV2.PlayMode)1, (PlayState)1, 0);
                ChosePlayerByType(PlayerType.Radio);
                PlayerManager.Instance.Resumed2();
            }
        }

        private static readonly Lazy<PlayerService> LazyInstance = new Lazy<PlayerService>(() =>
        {
            return new PlayerService();
        }, LazyThreadSafetyMode.ExecutionAndPublication);
        public static PlayerService Instance => LazyInstance.Value;

        private IPlayer musicPlayer = new MusicPlayer();
        //private IPlayer videoPlayer = new VideoPlayer();
        private IPlayer radioPlayer = XiaoweiPlayer.Instance;

        public IPlayer GetPlayerByType(PlayerType playerType)
        {
            switch (playerType)
            {
                case PlayerType.Music:
                    return musicPlayer;
                //case PlayerType.Video:
                //    return videoPlayer;
                case PlayerType.Radio:
                    return radioPlayer;
                default:
                    break;
            }
            return null;
        }

        public void ChosePlayerByType(PlayerType playerType)
        {
            PlayerManager.Instance.CurrentPlayer = GetPlayerByType(playerType);
        }

        public enum PlayerType
        {
            Music,
            Video,
            Radio
        }
    }
}
